package development.game;

import java.util.Random;

import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import development.Constants;
import development.DrawUtils;
import development.FrmDownloadResource;
import development.Global;
import development.listener.DownloadResourceListener;

public class FrmCuahang extends GameForm {
  private GameImage[] imageImages;
  private int selectedIndex;
  private boolean isSelectAll;
  private int[] cost;
  private String message;
  private GameImage bosoImg;
  private GameImage[] bosoImage;
  private boolean[] isBought;
  private GameImage buyallImg1;
  private GameImage buyallImg2;
  private String mess;
  private String[] str;
  private ImageText imgtext;
  private GameImage cuakeImg;
  private GameImage bgImage;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmCuahang();
    }
    return singleton;
  }

  public void init(Hashtable inputParameters) {
	  imgtext = FontManager.getFont(FontManager.FONT_SIZE_8);
    cost = new int[5];
    mess = "Xin lỗi, bạn không đủ tiền!";
    str = new String[] { "Nhấn mũi tên lên để hủy vật đang kéo", "Tốc độ kéo sẽ nhanh hơn. Chỉ có tác dụng trong một cửa", "Tỷ lệ may mắn khi kéo các túi quà sẽ tăng lên",
        "Kim cương sẽ có giá trị hơn. Chỉ có tác dụng trong một cửa", "Đá sẽ có giá trị gấp 3 lần. Chỉ có tác dụng trong một cửa" };
    message = str[0];
    Random rd = new Random();
    cost[0] = rd.nextInt(200);
    cost[1] = rd.nextInt(350) + 100;
    cost[2] = rd.nextInt(200) + 70;
    cost[3] = rd.nextInt(200) + 300;
    cost[4] = rd.nextInt(100);
    isBought = new boolean[5];
    for (int i = 0; i < 5; i++) {
      isBought[i] = false;
    }
    isSelectAll = false;
   
    imageImages = new GameImage[9];
    bosoImage = new GameImage[11];
    for (int i = 0; i < 9; i++) {
      imageImages[i] = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, (i + 1) + ".png");
    }
    if (Constants.IS_240x320_SCREEN) {
      bgImage = imageImages[7];
    } else {
      bgImage = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "88.png");
    }
    buyallImg2 = imageImages[6];
    buyallImg1 = imageImages[5];
    bosoImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "boso.png");
    for (int i = 0; i < 11; i++) {
      bosoImage[i] = ImageUtil.getSubImage(bosoImg, 9 * i, 0, 8, 12, true);
    }
    // cuakeImg = Image.createImage("/shop/cuake2.png");
    cuakeImg = ImageUtil.getImage(Constants.DAO_VANG_IMAGE_CATEGORY, "cuake2.png");
    selectedIndex = 0;
    DrawUtils.setBottomBar(_manager, "", "Mua", "Cửa kế");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    g.drawImage(bgImage, 0, 0, Constants.TOP_LEFT_ANCHOR);
    // g.drawImage(gamenameImgs, Constants.SCREEN_WIDTH/2, 40, Constants.CENTER_ANCHOR);
    for (int i = 0; i < 5; i++) {
      if (!isBought[i]) {
        g.drawImage(imageImages[i], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * i, 115, Constants.BOTTOM_LEFT_ANCHOR);
      }

    }
    if (!isBought[0]) {
      String str0 = String.valueOf(cost[0]);
      for (int i = 0; i < str0.length(); i++) {
        String c = String.valueOf(str0.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * 0 + i * 9, 120, Constants.TOP_LEFT_ANCHOR);
      }
    }

    if (!isBought[1]) {
      String str2 = String.valueOf(cost[1]);
      for (int i = 0; i < str2.length(); i++) {
        String c = String.valueOf(str2.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * 1 + i * 9, 120, Constants.TOP_LEFT_ANCHOR);
      }
    }

    if (!isBought[2]) {
      String str3 = String.valueOf(cost[2]);
      for (int i = 0; i < str3.length(); i++) {
        String c = String.valueOf(str3.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * 2 + i * 9, 120, Constants.TOP_LEFT_ANCHOR);
      }
    }

    if (!isBought[3]) {
      String str4 = String.valueOf(cost[3]);
      for (int i = 0; i < str4.length(); i++) {
        String c = String.valueOf(str4.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * 3 + i * 9, 120, Constants.TOP_LEFT_ANCHOR);
      }
    }

    if (!isBought[4]) {
      String str5 = String.valueOf(cost[2]);
      for (int i = 0; i < str5.length(); i++) {
        String c = String.valueOf(str5.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 10 + (Constants.SCREEN_WIDTH - 20) / 5 * 4 + i * 9, 120, Constants.TOP_LEFT_ANCHOR);
      }
    }

    // ve tong so diem
    String str1 = String.valueOf(FrmDaoVangOffline.score);
    if (Constants.IS_240x320_SCREEN) {
      for (int i = 0; i < str1.length(); i++) {
        String c = String.valueOf(str1.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 15 + (Constants.SCREEN_WIDTH - 20) / 5 * 2 + i * 9, 150, Constants.TOP_HCENTER_ANCHOR);
      }
      if (!isSelectAll) {
        g.drawImage(buyallImg1, Constants.SCREEN_WIDTH / 2, 175, Constants.TOP_HCENTER_ANCHOR);
        g.drawImage(imageImages[8], 8 + (Constants.SCREEN_WIDTH - 20) / 5 * selectedIndex, 117, Constants.BOTTOM_LEFT_ANCHOR);

      } else {
        g.drawImage(buyallImg2, Constants.SCREEN_WIDTH / 2, 175, Constants.TOP_HCENTER_ANCHOR);
      }
      imgtext.drawParagraph(g, message, GameColor.BLACK_CODE, Constants.SCREEN_WIDTH - 15, 10, 222);
      g.drawImage(cuakeImg, Constants.SCREEN_WIDTH - 5, Constants.SCREEN_HEIGHT - 5, Constants.BOTTOM_RIGHT_ANCHOR);
    } else {
      for (int i = 0; i < str1.length(); i++) {
        String c = String.valueOf(str1.charAt(i));
        g.drawImage(bosoImage[NumberUtil.parseInt(c) + 1], 15 + (Constants.SCREEN_WIDTH - 20) / 5 * 2 + i * 9, 50, Constants.TOP_HCENTER_ANCHOR);
      }
      if (!isSelectAll) {
        g.drawImage(buyallImg1, Constants.SCREEN_WIDTH / 2, 132, Constants.TOP_HCENTER_ANCHOR);
        g.drawImage(imageImages[8], 8 + (Constants.SCREEN_WIDTH - 20) / 5 * selectedIndex, 117, Constants.BOTTOM_LEFT_ANCHOR);

      } else {
        g.drawImage(buyallImg2, Constants.SCREEN_WIDTH / 2, 132, Constants.TOP_HCENTER_ANCHOR);
      }
      imgtext.drawParagraph(g, message, GameColor.BLACK_CODE, Constants.SCREEN_WIDTH - 15, 10, 165);
      g.drawImage(cuakeImg, Constants.SCREEN_WIDTH - 5, Constants.SCREEN_HEIGHT - 5, Constants.BOTTOM_RIGHT_ANCHOR);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      int x1 = 0;
      if (!isBoughtAll() && !isSelectAll) {
        if (isFirstItem(selectedIndex)) {

        } else {
          for (int i = selectedIndex - 1; i >= 0; i--) {
            if (!isBought[i]) {
              x1 = i;
              selectedIndex = x1;
              message = str[selectedIndex];
              break;
            } else {

            }
          }
        }
      }
      break;
    case Key.RIGHT:
      int x = 0;
      if (!isBoughtAll() && !isSelectAll) {
        if (isLastItem(selectedIndex)) {

        } else {
          for (int i = selectedIndex + 1; i < 5; i++) {
            if (!isBought[i]) {
              x = i;
              selectedIndex = x;
              message = str[selectedIndex];
              break;
            } else {

            }
          }
        }
      }

      break;
    case Key.UP:
      isSelectAll = !isSelectAll;
      break;
    case Key.DOWN:
      isSelectAll = !isSelectAll;
      break;
    case Key.FIRE:
      if (!isSelectAll) {
        if (selectedIndex == 0) {
          if (!isBought[0]) {
        	  FrmDaoVangOffline.f3 = 1;
            if (FrmDaoVangOffline.score >= cost[selectedIndex]) {
            	FrmDaoVangOffline.score -= cost[selectedIndex];
              isBought[0] = true;

            } else {
              message = mess;
            }
          }

        } else if (selectedIndex == 1) {
          if (!isBought[1]) {
        	  FrmDaoVangOffline.f1 = true;
            if (FrmDaoVangOffline.score >= cost[selectedIndex]) {
            	FrmDaoVangOffline.score -= cost[selectedIndex];
              isBought[1] = true;
            } else {
              message = mess;
            }
          }

        } else if (selectedIndex == 2) {
          if (!isBought[2]) {
        	  FrmDaoVangOffline.f5 = true;
            if (FrmDaoVangOffline.score >= cost[selectedIndex]) {
            	FrmDaoVangOffline.score -= cost[selectedIndex];
              isBought[2] = true;
            } else {
              message = mess;
            }
          }

        } else if (selectedIndex == 3) {
          if (!isBought[3]) {
        	  FrmDaoVangOffline.f4 = true;
            if (FrmDaoVangOffline.score >= cost[selectedIndex]) {
            	FrmDaoVangOffline.score -= cost[selectedIndex];
              isBought[3] = true;
            } else {
              message = mess;
            }
          }
        } else {
          if (!isBought[4]) {
        	  FrmDaoVangOffline.f2 = true;
            if (FrmDaoVangOffline.score >= cost[selectedIndex]) {
            	FrmDaoVangOffline.score -= cost[selectedIndex];
              isBought[4] = true;
            } else {
              message = mess;
            }
          }
        }
      } else {
        int sum = 0;
        for (int i = 0; i < 5; i++) {
          if (!isBought[i]) {
            sum += cost[i];
          }
        }
        if (FrmDaoVangOffline.score >= sum) {
        	FrmDaoVangOffline.score = FrmDaoVangOffline.score - sum;
          for (int i = 0; i < 5; i++) {
            isBought[i] = true;
          }
        } else {
          message = mess;
        }
      }
      break;
    case Key.SOFT_RIGHT:
    	FrmDaoVangOffline.level = (FrmDaoVangOffline.level + 1) % 20;
      DownloadResourceListener downloadResourceListener = new DownloadResourceListener(Constants.DAO_VANG_IMAGE_CATEGORY);
      downloadResourceListener.nextForm = FrmDaoVangOffline.instance();
      Global.authenticateHandle.addListener(downloadResourceListener);
      GameGlobal.nextState(FrmDownloadResource.instance(), null);
      break;
    default:
      break;
    }
  }

  private boolean isBoughtAll() {
    int dem = 0;
    for (int i = 0; i < 5; i++) {
      if (isBought[i]) {
        dem++;
      }
    }
    if (dem == 5) {
      return true;
    } else {
      return false;
    }
  }

  private boolean isLastItem(int index) {
    // int dem = 0;
    boolean b;
    if (index == 4) {
      b = true;
    } else {
      for (int i = index + 1; i < 5; i++) {
        if (!isBought[i]) {
          b = false;
          return false;
        }
      }
      b = true;
    }

    return b;
  }

  private boolean isFirstItem(int index) {
    // int dem = 0;
    boolean b;
    if (index == 0) {
      b = true;
    } else {
      for (int i = index - 1; i >= 0; i--) {
        if (!isBought[i]) {
          b = false;
          return false;
        }
      }
      b = true;
    }
    return b;
  }

  public void destroy() {
    imageImages = null;
    cost = null;
    message = null;
    bosoImg = null;
    bosoImage = null;
    isBought = null;
    str = null;
    imgtext = null;
    cuakeImg = null;
    buyallImg1 = null;
    buyallImg2 = null;
    str = null;
    buyallImg1 = null;
    buyallImg2 = null;
    mess = null;
    bgImage = null;
  }
}
